package com.yixin.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.domain.Dictionary;
import com.yixin.hospital.entity.dto.req.dictionary.DictionaryAddReqDTO;
import com.yixin.hospital.entity.dto.req.dictionary.DictionaryEditReqDTO;
import com.yixin.hospital.mapper.DictionaryMapper;
import com.yixin.hospital.service.DictionaryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author wb-wangfushun
* @description 针对表【dictionary(字典表)】的数据库操作Service实现
* @createDate 2024-04-05 21:38:00
*/
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary>
    implements DictionaryService {

    @Override
    public ApiResult allList(Integer type) {
        LambdaQueryWrapper<Dictionary> deptQueryWrapper = new LambdaQueryWrapper<>();
        deptQueryWrapper.eq(Dictionary::getType, type);
        deptQueryWrapper.orderByAsc(Dictionary::getSort);
        List<Dictionary> list = this.list(deptQueryWrapper);
        return ApiResult.success(list);
    }

    @Override
    public ApiResult add(DictionaryAddReqDTO jobAddReqDTO, String userName) {
        Dictionary dictionary = new Dictionary();
        dictionary.setType(jobAddReqDTO.getType());
        dictionary.setName(jobAddReqDTO.getName());
        dictionary.setCreatedBy(userName);
        dictionary.setUpdatedBy(userName);
        long sort = this.count(new LambdaQueryWrapper<Dictionary>().eq(Dictionary::getType, jobAddReqDTO.getType()));
        dictionary.setSort((int)sort+1);
        this.save(dictionary);
        return ApiResult.success();
    }

    @Override
    public ApiResult edit(DictionaryEditReqDTO jobEditReqDTO, String userName) {
        Dictionary dictionary = new Dictionary();
        dictionary.setId(jobEditReqDTO.getId());
        dictionary.setType(jobEditReqDTO.getType());
        dictionary.setName(jobEditReqDTO.getName());
        dictionary.setSort(jobEditReqDTO.getSort());
        dictionary.setCreatedBy(userName);
        dictionary.setUpdatedBy(userName);
        this.updateById(dictionary);
        return ApiResult.success();
    }
}
